<template>
  <div></div>
  <el-card>
    <Category :scene="scene"></Category>
  </el-card>
  <el-card style="margin: 10px 0">
    <div v-show="scene === 0">
      <el-button
        @click="addSpu"
        type="primary"
        icon="Plus"
        :disabled="!categoryStore.categoryIdObj.category3Id"
      >
        添加SPU
      </el-button>
      <el-table :data="spuData" style="margin: 10px 0" border>
        <el-table-column
          type="index"
          label="序号"
          width="80"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="spuName"
          label="SPU名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="description"
          show-overflow-tooltip
          label="SPU描述"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template #="{ row }">
            <el-button
              type="primary"
              size="small"
              icon="Plus"
              title="添加SKU"
              @click="addSku"
            ></el-button>
            <el-button
              type="primary"
              size="small"
              icon="Edit"
              title="修改SPU"
              @click="updateSpu(row)"
            ></el-button>
            <el-button
              type="info"
              size="small"
              icon="View"
              title="查看SKU"
              @click="findSku(row)"
            ></el-button>
            <el-popconfirm
              :title="`你确定删除${row.spuName}?`"
              width="200px"
              @confirm="deleteSpu(row)"
            >
              <template #reference>
                <el-button
                  type="primary"
                  size="small"
                  icon="Delete"
                  title="删除SPU"
                ></el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        style="width: 100%"
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[3, 5, 7, 9]"
        :background="true"
        layout="prev, pager, next, jumper, ->, sizes, total"
        :total="total"
        @current-change="getSpuList"
        @size-change="changeSizePage"
      />
    </div>

    <!-- 添加|修改spu -->
    <SpuForm
      ref="spuFormRef"
      v-show="scene === 1"
      @changeScene="changeScene"
    ></SpuForm>
    <!-- 添加SKU子组件 -->
    <SkuForm v-show="scene === 2" @changeScene="changeScene"></SkuForm>

    <!-- dialog对话框:展示已有的SKU数据 -->
    <el-dialog v-model="show" title="SKU列表">
      <el-table border :data="skuArr">
        <el-table-column label="SKU名字" prop="skuName"></el-table-column>
        <el-table-column label="SKU价格" prop="price"></el-table-column>
        <el-table-column label="SKU重量" prop="weight"></el-table-column>
        <el-table-column label="SKU图片">
          <template #="{ row }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </el-card>
</template>

<script lang="ts" setup>
import { ref, watch } from 'vue'
import useCategoryStore from '@/store/modules/category'
import { reqHasSpu, reqSkuList, reqRemoveSpu } from '@/api/product/spu/index'
import type {
  SpuList,
  SpuResponseData,
  Spu,
  SkuInfoData,
  SkuData,
} from '@/api/product/spu/types'
import { ElMessage } from 'element-plus'
import SpuForm from './spuForm.vue'
import SkuForm from './skuForm.vue'

let currentPage = ref(1)
let pageSize = ref(3)
let total = ref(0)
let show = ref<boolean>(false)
let scene = ref(0)
let spuData = ref<SpuList>([])
//存储全部的SKU数据
let skuArr = ref<SkuData[]>([])
let spuFormRef = ref()

const categoryStore = useCategoryStore()
watch(
  () => categoryStore.categoryIdObj.category3Id,
  () => {
    if (!categoryStore.categoryIdObj.category3Id) return
    getSpuList()
  },
)

async function getSpuList(pager = 1) {
  currentPage.value = pager
  let res: SpuResponseData = await reqHasSpu(
    currentPage.value,
    pageSize.value,
    categoryStore.categoryIdObj.category3Id,
  )
  if (res.code == 200) {
    spuData.value = res.data.records
    total.value = res.data.total
  }
}

function changeSizePage() {
  getSpuList()
}

function changeScene(number: number) {
  scene.value = number
  getSpuList()
}

function updateSpu(row: Spu) {
  scene.value = 1
  spuFormRef.value.initHasSpu(row)
}

function addSpu() {
  scene.value = 1
  spuFormRef.value.initAddSpu(categoryStore.categoryIdObj.category3Id)
}
function addSku() {
  scene.value = 2
}

async function findSku(row: Spu) {
  let result: SkuInfoData = await reqSkuList(row.id as number)
  if (result.code == 200) {
    skuArr.value = result.data
    //对话框显示出来
    show.value = true
  }
}

async function deleteSpu(row: Spu) {
  let result: any = await reqRemoveSpu(row.id as number)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    //获取剩余SPU数据
    getSpuList(
      spuData.value.length > 1 ? currentPage.value : currentPage.value - 1,
    )
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
</script>

<style scoped></style>
